package controllers;

import beans.*;
import play.*;
import play.libs.Codec;
import play.mvc.*;

import java.util.*;

import models.*;
import utils.Config;
import utils.SessionUtils;
import utils.StringUtils;

public class Application extends Controller {

    public static void article(String id,int page) {
        String host = Config.Host;
        Article article = Article.getArticleById(id);
        String userId = SessionUtils.getUserId();
        if(article!=null)
        {
            article.click++;
            article.save();
        }
        String [] tagList = null;
        if(StringUtils.isNotEmpty(article.tag))
        {
            tagList = article.tag.split(" ");
        }
        User user = User.findById(article.authorId);
        long total = ArticleComment.count("articleId = ?",id);
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total, 5);
        List<ArticleComment> commentList = ArticleComment.find("articleId = ? order by createTime desc", id).
                fetch(pageBean.getCurPage(), pageBean.getPerPage());
        WebServiceBean webServiceBean = WebServiceBean.getSuccessInstance();
        webServiceBean.data = ArticleCommentBean.buildList(commentList);
        Advertisement advertisement = Advertisement.find("status = 0").first();

        List<User> userList = User.find("order by fansNum desc").fetch(10);
        List<SimpleUserBean> result = new ArrayList<SimpleUserBean>();
        for(int i = 0;i < userList.size();i++)
        {
            SimpleUserBean bean = new SimpleUserBean();
            bean.id = userList.get(i).id;
            bean.nickName = userList.get(i).nickname;
            result.add(bean);
        }
        List<Album> albumList = Album.find(
                "authorId = ? and isDelete != 1 order by createTime desc",article.authorId).fetch();
        List<ArticleComment> commentList_new = ArticleComment.find("order by createTime desc").fetch(10);
        List<SimpleArticleCommentBean> result_new = new ArrayList<SimpleArticleCommentBean>();
        for(int i = 0;i < commentList_new.size();i++)
        {
            SimpleArticleCommentBean bean = new SimpleArticleCommentBean();
            bean.articleId = commentList_new.get(i).articleId;
            bean.content = commentList_new.get(i).comment;
            result_new.add(bean);
        }
        /*if(article.attachmentId!=null){
            Attachment attachment = Attachment.findById(article.attachmentId);
            render("Members/article.html",user,article,attachment,host,userId,tagList,webServiceBean,advertisement);
        }
        else{*/
         render("Members/article.html",user,article,host,userId,tagList,webServiceBean,advertisement,result,result_new,albumList);
       // renderJSON(result_new);
     //   }
    }
    public static void index(int type,int news) {
        String host = Config.Host;
        if(type==0){
            type=-1;
        }
       List<RecommendBean> list = Recommend.getRecommend(4);
        if (news==1){
            render(type,news,list,host);
        }
        else{
            news = 0;
            render(type,news,list,host);
        }
    }
    public static void about() {
        WebsiteInfo info =WebsiteInfo.find("").first();
        String cont = info.aboutUs;
        render(cont);
    }
    public static void register() {
        List<TypeName> list = TypeName.findAll();
        String randomID = Codec.UUID();
        System.out.println("registrt_randomID = "+randomID);
        render(randomID,list);    }
    public static void employ() {
        WebsiteInfo info =WebsiteInfo.find("").first();
        String cont = info.joinUs;
        render(cont);    }
    public static void callUs() {
        WebsiteInfo info =WebsiteInfo.find("").first();
        String cont = info.contactUs;
        render(cont);    }
    public static void feedback() {
        WebsiteInfo info =WebsiteInfo.find("").first();
        String cont = info.suggestion;
        render(cont);     }
    public static void declare() {
        WebsiteInfo info =WebsiteInfo.find("").first();
        String cont = info.disclaimer;
        render(cont);     }
    public static void friend() {
        WebsiteInfo info =WebsiteInfo.find("").first();
        String cont = info.friend;
        render(cont);     }
    public static void login() {
        render();
    }
    public static void activity(int type) {
        List<RecommendBean> list = Recommend.getRecommend(4);
        List<RecommendBean> activityList = Recommend.getActivity(4);
        render(list,activityList,type);
    }
    /**
     *@author 李宇星
     *模糊搜索文章
     **/
    public static void search(String title,int page)
    {
        List<RecommendBean> list = Recommend.getRecommend(4);
        long total = Article.count("isDelete != 1 and is_verify = 1 and title like '%" + title + "%'");
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total,8);
        List<Article> articleList = Article.find("isDelete != 1 and is_verify = 1 and title like '%" + title + "%'").
                fetch(pageBean.getCurPage(), pageBean.getPerPage());
        WebServiceBean webServiceBean = WebServiceBean.getSuccessInstance();
        webServiceBean.data = ArticleBean.buildList(articleList);
        render(webServiceBean,list,pageBean);
    }

    public static void activityItem(String id) {
        Recommend recommend = Recommend.find("id= ?",id).first();
        render(recommend);
    }

}